
package com.hlingsoft.cms.action;


import com.hlingsoft.cms.entity.Brand;
import com.hlingsoft.cms.entity.Goods;
import com.hlingsoft.cms.entity.Image;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hlingsoft.cms.exception.TemplateNotFoundException;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
 * 首页
 *
 * @author Herbert
 */
@Controller
public class IndexAction extends BaseAction {


    /**
     * 首页
     *
     * @param p
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String defalut(
            @RequestParam(value = "p", defaultValue = "1") long p,
            ModelMap modelMap) {
        return home(p, modelMap);
    }

    /**
     * 首页
     *
     * @param p
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/index.htm", method = RequestMethod.GET)
    public String home(@RequestParam(value = "p", defaultValue = "1") long p,
                       ModelMap modelMap) {
        try {
            modelMap.addAttribute("p", p);
            modelMap.addAttribute("g_folderId", 0);
            return themeService.getDefaultTemplate();
        } catch (TemplateNotFoundException e) {
            modelMap.addAttribute("g_folderId", 0);
            logger.fatal(e.getMessage());
            return themeService.get404();
        }
    }

    /**
     * 404
     *
     * @return
     */
    @RequestMapping(value = "/404.htm", method = RequestMethod.GET)
    public String pageNotFound(ModelMap modelMap) {
        modelMap.addAttribute("g_folderId", 0);
        return themeService.get404();
    }

    /**
     * 500
     *
     * @return
     */
    @RequestMapping(value = "/500.htm", method = RequestMethod.GET)
    public String error(ModelMap modelMap) {
        modelMap.addAttribute("g_folderId", 0);
        return themeService.get500();
    }


    /**
     * 首页修改版本
     *
     * @param categoryId 1.今日精选
     *                   2.达人设计
     * @param request
     * @return
     */
    @RequestMapping(value = "/index.json")
    @ResponseBody
    public Map<String, Object> index(
            @RequestParam(value = "categoryId") int categoryId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            if (categoryId == 1) {
                List<Goods> goodsList = goodsDao.getGoodsListByCategoryId(1);
                for (Goods goods : goodsList) {
                    List<Image> imageList = imageDao.getImageByGoodsId(goods.getId(), 0);
                    String image = imageList.get(0).getPath();
                    goods.setImage(image);
                }
                resultMap.put("goodsList", goodsList);
            } else if (categoryId == 2) {
                List<Brand> brandList = brandDao.getBrandList();
                resultMap.put("brandList", brandList);
            }
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 获取品牌的商品
     *
     * @param brandId
     * @param request
     * @return
     */
    @RequestMapping(value = "/getBrand.json")
    @ResponseBody
    public Map<String, Object> getBrand(
            @RequestParam(value = "brandId") int brandId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Brand brand = brandDao.getBrandById(brandId);
            if (brand != null) {
                String goodBanner = brand.getGoodBanner();
                List<Goods> goodsList = goodsDao.getGoodsByBrandId(brandId);

                for (Goods goods : goodsList) {
                    List<Image> imageList = imageDao.getImageByGoodsId(goods.getId(), 0);
                    String image = imageList.get(0).getPath();
                    goods.setImage(image);
                    goods.setManufacturer(brand.getName());
                    goods.setBrandLogo(brand.getLogo());

                }

                resultMap.put("goodsList", goodsList);
                resultMap.put("goodBanner", goodBanner);
                resultMap.put("result", true);


            } else {
                resultMap.put("msg", "您所选的品牌不存在");
                resultMap.put("result", false);
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 获取未读信息
     *
     * @param appUserId
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUnreadMessage.json")
    @ResponseBody
    public Map<String, Object> getUnreadMessage(
            @RequestParam(value = "appUserId") long appUserId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            int unreadCommentMessage = messageStatusDao.getUnreadCommentMessageCount(appUserId);
            int unreadLikesMessage = messageStatusDao.getUnreadLikesMessageCount(appUserId);
            int unreadFocusMessage = messageStatusDao.getUnreadFocusMessageCount(appUserId);
            int unreadSystemMessage = messageStatusDao.getUnreadSystemMessageCount(appUserId);

            int unreadCount = unreadCommentMessage + unreadLikesMessage + unreadFocusMessage + unreadSystemMessage;


            List<Integer> unreadList = new ArrayList<Integer>();
            unreadList.add(unreadCommentMessage);
            unreadList.add(unreadLikesMessage);
            unreadList.add(unreadFocusMessage);
            unreadList.add(unreadSystemMessage);


            resultMap.put("unreadList", unreadList);
            resultMap.put("unreadCount", unreadCount);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }

}
